package com.bjsxt.user.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.commom.util.DateFormatUtil;
import com.bjsxt.commom.util.PageInfoUtil;
import com.bjsxt.user.dao.IAAdminsDao;
import com.bjsxt.user.pojo.AAdmins;
import com.bjsxt.user.service.IUserService;
@Service("userService")
public class UserServiceImpl implements IUserService
{
	@Resource
	private IAAdminsDao adminsDao;
	
	@Resource
	private DateFormatUtil dateFormatUtil;
	/* ===================管理员操作开始======================= */
	@Override
	public JSONObject insertAdminsService(AAdmins admins)
	{
		JSONObject resultJson = new JSONObject();
		/* 先查重 */
		Map<String, Object> sourceMap = new HashMap<String, Object>();
		sourceMap.put("email", admins.getEmail());
		sourceMap.put("password", admins.getPassword());
		AAdmins temp = this.findOneAdminsService(sourceMap);
		if(temp != null)
		{
			resultJson.put("code", 2);
			resultJson.put("info", "邮箱已存在");
			return resultJson;
		}
		int insert = this.adminsDao.insert(admins);
		if(insert>0)
		{
			resultJson.put("code", 0);
			resultJson.put("info", "管理员添加成功");
			resultJson.put("data", admins.getId());
		}else{
			resultJson.put("code", 1);
			resultJson.put("info", "管理员添加失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteAdminsService(AAdmins admins)
	{
		JSONObject resultJson = new JSONObject();
		int delete = this.adminsDao.delete(admins);
		if(delete>0)
		{
			resultJson.put("code", 0);
			resultJson.put("info", "管理员删除成功");
			resultJson.put("data", delete);
		}else{
			resultJson.put("code", 1);
			resultJson.put("info", "管理员删除失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateAdminsService(AAdmins admins)
	{
		JSONObject resultJson = new JSONObject();
		int update = this.adminsDao.update(admins);
		if(update>0)
		{
			resultJson.put("code", 0);
			resultJson.put("info", "管理员更新成功");
			resultJson.put("data", update);
		}else{
			resultJson.put("code", 1);
			resultJson.put("info", "管理员跟新失败");
		}
		return resultJson;
	}

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public List<AAdmins> findListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{	
		/**
		 * pageInfo == true ;表示分页
			createTimeAsc == asc ; 表示创建时间倒叙查询(默认按照创建时间正序)
			updateTimeSelect == true ; 表示按照更新时间查询(默认表示正序)
			updateTimeSelectAsc == asc ; 表示按照更新时间倒叙查询
		 */
		if(pageInfoUtil == null)
		{
			return this.adminsDao.findList(condMap);
		}else
		{
			/* 先查询总计数 */
			condMap.put("pageInfo","true");
			List<AAdmins> list = this.adminsDao.findList(condMap);
			/*  */
			pageInfoUtil.setTotalRecord(list.get(0).getId());
			condMap.put("pageInfo", "false");
			condMap.put("currRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return  this.adminsDao.findList(condMap);
		}
	}
	/* ===================管理员操作结束======================= */
}
